package com.example.administrator.myapplication;

import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class MainActivity extends AppCompatActivity {
    private String TAG = "MainActivity";
    private BufferedReader bufferedReader;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        initEvent();
    }
    private void initView(){

    }
    private void initData(){
//        //内部存储
        String filename = "data.txt";
        String content = "何盈201965110422";
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = openFileOutput(filename,MODE_PRIVATE);//用于打开应用程序的输出流，将数据存储到指定文件中
            fileOutputStream.write(content.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }  finally {
            if (fileOutputStream!=null){
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        //外部存储
        String state = Environment.getExternalStorageState();
        Log.e(TAG,"initData:"+state);
        if (state.equals(Environment.MEDIA_MOUNTED)){
            File SDfile = Environment.getExternalStorageDirectory();//获取外部存储目录
            File file = new File(SDfile,"data.txt");
            String data = "何盈201965110415";
            FileOutputStream fileOutputStream1 = null;
            try {
                fileOutputStream1 = new FileOutputStream(file);
                fileOutputStream1.write(data.getBytes());
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (fileOutputStream1!=null){
                    try {
                        fileOutputStream1.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        //读取内部存储中的文件数据
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = openFileInput("data.txt");
            byte[] bytes = new byte[fileInputStream.available()];
            fileInputStream.read(bytes);
            content = new String(bytes);
            Toast.makeText(this,content+"",Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 读取外部存储中的文件数据
        state = Environment.getExternalStorageState();
        if (state.equals(Environment.MEDIA_MOUNTED)){
            File SDPath = Environment.getExternalStorageDirectory();
            File file = new File(SDPath,"data.txt");
            fileInputStream = null;
            try {
                fileInputStream = new FileInputStream(file);
                bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
                String data = bufferedReader.readLine();
                Toast.makeText(this,data+"",Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                if (bufferedReader != null){
                    try {
                        bufferedReader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (fileInputStream !=null){
                    try {
                        fileInputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

        }
    }

    private void initEvent(){

    }
}
